package com.example.myanimator.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.RegionIterator;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @author tgw
 * @date 2021/3/16
 * @describe
 */
class DrawTextPath extends View {

    private Paint mPaint;
    private Path mPath;
    private Canvas mCanvas;
    private String tipStr = "床前明月光";
    private Rect tipBounds = new Rect();

    public DrawTextPath(Context context) {
        super(context);
    }

    public DrawTextPath(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 去除锯齿
        mPaint.setStrokeWidth(5);//设置笔画宽度
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setTextAlign(Paint.Align.CENTER);//设置文字对齐方式
        mPaint.setTextSize(50);//设置文字大小
        //
        mPaint.setFakeBoldText(true);//是否为粗体
        mPaint.setUnderlineText(true);//设置下划线
        mPaint.setTextSkewX((float) -0.25);//设置字体水平倾斜度，普通斜体字设为-0.25
        mPaint.setStrikeThruText(true);//设置带有删除线效果
        mPaint.setTextScaleX(2);//水平方向字体拉伸，高度不变
        mPaint.setColor(Color.WHITE);
        mPath = new Path();
        mCanvas = new Canvas();
        mPaint.getTextBounds(tipStr, 0, tipStr.length(), tipBounds);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawText(tipStr,0,1,tipBounds.centerX(),tipBounds.height(),mPaint);
        mPaint.setTextAlign(Paint.Align.LEFT);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawText(tipStr,tipBounds.centerX(),tipBounds.height()+100,mPaint);
        mPaint.setTextAlign(Paint.Align.RIGHT);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawText(tipStr,tipBounds.centerX(),tipBounds.height()+200,mPaint);

//        mPath.addCircle(tipBounds.width()/2,1000,tipBounds.width()/2, Path.Direction.CCW);
//        canvas.drawTextOnPath(tipStr,mPath,0,0,mPaint);
        Typeface typeface =  Typeface.create("宋体",Typeface.NORMAL);//使用系统中的字体 ，如果宋体不存在返回默认字体
        mPaint.setTypeface(typeface);
        mPath.addCircle(tipBounds.width()/2,1000,tipBounds.width()/2, Path.Direction.CW);
        canvas.drawTextOnPath(tipStr,mPath,0,0,mPaint);

        Region region = new Region(new Rect(50,50,200,100));
        RegionIterator regionIterator = new RegionIterator(region);

        while (regionIterator.next(tipBounds)){
            canvas.drawRect(tipBounds,mPaint);
        }
    }
}
